package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("MotiveController")
public class MotiveController extends CmiEmaController {

	@RequestMapping("/listMotives")
	public ModelAndView listMotives(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = motivePaginationService.updatePagination(request, pagination);
		Set<Motive> motives = motivePaginationService.getPaginationMotives(pagination);

		mav.addObject("motives", motives);
		mav.addObject("entityName", "motive");
		mav.addObject("pagination", pagination);
		mav.setViewName("motive/listMotives.jsp");
		return mav;
	}

	public String listMotives() {
		return "redirect:/listMotives";
	}

	@RequestMapping("/viewMotive")
	public ModelAndView viewMotive(@RequestParam Integer motiveId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("motive", motiveDAO.findMotiveByPrimaryKey(motiveId));
		mav.setViewName("motive/viewMotive.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteMotives")
	public ModelAndView confirmDeleteMotives(
			@RequestParam(value = "deleteMotivesIds", required = true) Set<Integer> deleteMotivesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Motive> deleteMotives = motiveDAO.findMotivesByIds(deleteMotivesIds);
		Set<Motive> referencedMotives = motiveService.loadReferenceMotives(deleteMotives);
		boolean hasReferenced;
		if (referencedMotives.size() == 0) {
			mav.addObject("deleteEntitys", deleteMotives);
			hasReferenced = false;
		} else {
			deleteMotives.removeAll(referencedMotives);
			mav.addObject("referencedEntitys", referencedMotives);
			mav.addObject("deleteEntitys", deleteMotives);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Motive");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listMotives");
		return mav;
	}

	@RequestMapping("/deleteMotive")
	public String deleteMotive(@RequestParam Integer motiveId) {
		Motive motive = motiveDAO.findMotiveByPrimaryKey(motiveId);
		motiveService.deleteMotive(motive);
		return "forward:/listMotives";
	}

	@RequestMapping("/confirmDeleteMotive")
	public ModelAndView confirmDeleteMotive(@RequestParam Integer motiveId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("motive", motiveDAO.findMotiveByPrimaryKey(motiveId));

		mav.setViewName("motive/deleteMotive.jsp");

		return mav;
	}

	@RequestMapping("/deleteMotives")
	public String deleteMotives(
			@RequestParam(value = "deleteMotivesId", required = true) Set<Integer> deleteMotivesId) {
		motiveService.deleteMotives(deleteMotivesId);
		return "forward:/listMotives";
	}

	@RequestMapping("/editMotive")
	public ModelAndView editMotive(@RequestParam Integer motiveId) {

		ModelAndView mav = new ModelAndView();

		Motive motive = motiveDAO.findMotiveByPrimaryKey(motiveId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("motive", motive);

		// 把所有的引用motive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(motiveService.loadMotiveObjectReferencedSet(motive));

		// 将编辑页面motive添加到mav中
		mav.setViewName("motive/editMotive.jsp");
		return mav;
	}

	@RequestMapping("/newMotive")
	public ModelAndView newMotive() {
		ModelAndView mav = new ModelAndView();
		Motive motive = new Motive();

		mav.addObject("motive", motive);

		// 把所有的引用motive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(motiveService.loadMotiveObjectReferencedSet(motive));

		mav.addObject("newFlag", true);
		mav.setViewName("motive/editMotive.jsp");

		return mav;
	}

	@RequestMapping("/saveMotive")
	public ModelAndView saveMotive(@ModelAttribute Motive motive) {

		ModelAndView mav = new ModelAndView();

		Motive existingMotive = CmiHibernateProxyUtil
				.deproxy(motiveDAO.findMotiveByPhysicalPrimaryKey(motive.getName()));

		// 新建记录
		if (motive.getId() == null) {
			if (motive.equals(existingMotive)) {

				mav.addAllObjects(motiveService.loadMotiveClassReferencedSet(motive));
				// mav = editContinue(mav, motive);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("motive/editMotive.jsp");
			} else {
				motiveService.saveMotive(motive);
				mav.setViewName("forward:/listMotives");
			}
		}
		// 编辑记录
		else {
			Motive originMotive = CmiHibernateProxyUtil.deproxy(motiveDAO.findMotiveByPrimaryKey(motive.getId()));
			if (!motive.equals(originMotive) && motive.equals(existingMotive)) {
				mav.addAllObjects(motiveService.loadMotiveClassReferencedSet(motive));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("motive/editMotive.jsp");
			} else {
				motiveService.saveMotive(motive);
				mav.setViewName("forward:/listMotives");
			}
		}
		return mav;
	}

	@RequestMapping("/motiveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
